package cn.lixu.pet.controller;

import cn.lixu.basic.annotation.PreAuthorize;
import cn.lixu.basic.util.JsonResult;
import cn.lixu.basic.util.LoginContext;
import cn.lixu.basic.util.PageList;
import cn.lixu.org.domain.Logininfo;
import cn.lixu.pet.domain.Pet;
import cn.lixu.pet.query.PetQuery;
import cn.lixu.pet.service.IPetService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部")
    @PreAuthorize(name = "查询全部宠物列表", sn = "pet:list") //value后面用来做权限校验
    public List<Pet> loadAll(HttpServletRequest request) {
        //获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return petService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一个")
    @PreAuthorize(name = "查询一个宠物列表", sn = "pet:loadById") //value后面用来做权限校验
    public Pet loadById(@PathVariable("id") Long id) {
        return petService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            petService.remove(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param pet 添加和修改
     * @return
     */
    @PutMapping
    public JsonResult addAndUpdate(@RequestBody Pet pet) {
        try {
            if (pet.getId() == null) {
                petService.save(pet);
            } else {
                petService.update(pet);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param petQuery
     * @return
     */
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery petQuery) {
        return petService.queryPage(petQuery);

    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            petService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 宠物上架
     *
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public JsonResult onsale(@RequestBody List<Long> ids) {
        try {
            petService.onSale(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * 宠物下架
     *
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public JsonResult offsale(@RequestBody List<Long> ids) {
        try {
            petService.offSale(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

//    /**
//     * 查询宠物树
//     *
//     * @return
//     */
//    @GetMapping("/deptTree")
//    @ApiOperation(value = "获取宠物树-无限极接口")
//    public List<Pet> deptTree() {
//        return petService.deptTree();
//    }
}

